//
//  KQMessageCustomTipsContentView.swift
//  KangYunYouJia
//
//  Created by zhouxiang on 2020/12/17.
//  Copyright © 2020 zhoux@zzkqdc.com. All rights reserved.
//

import UIKit

class KQMessageCustomTipsContentView: UILabel, KQMessageContentViewType {

    public override init(frame: CGRect) {
        super.init(frame: frame)
        _commonInit()
    }
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        _commonInit()
    }

    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return super.canPerformAction(action, withSender: sender)
    }
    
    open func apply(_ message: KQMessageType) {
        _message = message
        guard let content = message.content as? KQMessageCustomTipsContent else {
            return
        }
        _delegate = content.delegate
        let attr = NSMutableAttributedString(string: "\(content.title ?? "")，点击此处查看>>")
        attr.addAttributes([NSAttributedString.Key.font: UIFont(name: KQCHAT_FONT_NAME, size: 12)!,
                            NSAttributedString.Key.foregroundColor: KQCHAT_COLOR(153, 153, 153)], range: NSMakeRange(0, attr.length - 8))
        attr.addAttributes([NSAttributedString.Key.font: UIFont(name: KQCHAT_FONT_NAME, size: 12)!,
                            NSAttributedString.Key.foregroundColor: KQCHAT_COLOR(2, 146, 186)], range: NSMakeRange(attr.length - 8, 8))
        self.attributedText = attr
        self.reapirId = content.repairId
    }
    
    private func _commonInit() {
        self.numberOfLines = 0
        self.textAlignment = .center
        self.isUserInteractionEnabled = true
        
        let button = UIButton()
        button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        self.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    weak var _delegate: KQMessageDelegate?
    private var _message: KQMessageType!
    private var reapirId: String?
    
    @objc func buttonClick(_ sender: UIButton) {
        _delegate?.tapCustom?(message: _message, nextType: .jumpRepair, param: self.reapirId)
    }

}
